package com.zhuoyue.system.service.impl;

import com.zhuoyue.system.domain.SysMovement;
import com.zhuoyue.system.domain.dto.SysMovementDTO;
import com.zhuoyue.system.mapper.SysMovementMapper;
import com.zhuoyue.system.service.ISysMovementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 企业动态Service业务层处理
 *
 * @author zhanghui
 * @date 2021-08-23
 */
@Service
public class SysMovementServiceImpl implements ISysMovementService {
    @Autowired
    private SysMovementMapper sysMovementMapper;

    /**
     * 查询企业动态
     *
     * @param id 企业动态ID
     * @return 企业动态
     */
    @Override
    public SysMovement selectSysMovementById(Long id) {
        return sysMovementMapper.selectSysMovementById(id);
    }

    /**
     * 查询企业动态列表
     *
     * @param sysMovement 企业动态
     * @return 企业动态
     */
    @Override
    public List<SysMovement> selectSysMovementList(SysMovement sysMovement) {
        return sysMovementMapper.selectSysMovementList(sysMovement);
    }

    @Override
    public List<SysMovementDTO> selectSysMovementListForApi(String orgCode) {
        List<SysMovementDTO> list = new ArrayList<>();

        // 查询某组织下发布状态的动态
        List<SysMovement> sysMovements = sysMovementMapper.selectSysMovementListForApp(orgCode);

        // 最多只取一条动态
        if (sysMovements.size() > 0) {
            sysMovements = sysMovements.subList(0, 1);
        }

        // DO转为DTO
        for (SysMovement movement : sysMovements) {
            SysMovementDTO movementDTO = new SysMovementDTO();
            movementDTO.setId(movement.getId());
            movementDTO.setDictValue(movement.getDictValue());
            movementDTO.setTitle(movement.getTitle());
            movementDTO.setContent(movement.getContent());
            movementDTO.setIconUrl(movement.getIconUrl());
            movementDTO.setRedirectUrl(movement.getRedirectUrl());
            movementDTO.setClickCount(movement.getClickCount());
            movementDTO.setSwiper(movement.getSwiper());
            movementDTO.setCreateTime(movement.getCreateTime().getTime());
            list.add(movementDTO);
        }

        return list;
    }

    /**
     * 新增企业动态
     *
     * @param sysMovement 企业动态
     * @return 结果
     */
    @Override
    public int insertSysMovement(SysMovement sysMovement) {
        return sysMovementMapper.insertSysMovement(sysMovement);
    }

    /**
     * 修改企业动态
     *
     * @param sysMovement 企业动态
     * @return 结果
     */
    @Override
    public int updateSysMovement(SysMovement sysMovement) {
        return sysMovementMapper.updateSysMovement(sysMovement);
    }

    /**
     * 批量删除企业动态
     *
     * @param ids 需要删除的企业动态ID
     * @return 结果
     */
    @Override
    public int deleteSysMovementByIds(Long[] ids) {
        return sysMovementMapper.deleteSysMovementByIds(ids);
    }

    /**
     * 删除企业动态信息
     *
     * @param id 企业动态ID
     * @return 结果
     */
    @Override
    public int deleteSysMovementById(Long id) {
        return sysMovementMapper.deleteSysMovementById(id);
    }

    @Override
    public int addClickCount(Long id, int count) {
        return sysMovementMapper.addClickCount(id, count);
    }

    @Override
    public int setStatusDisableOfOrgAndType(Long deptId, Long dictCode) {
        return sysMovementMapper.setStatusDisableOfOrgAndType(deptId, dictCode);
    }
}
